package com.buba.springcloud.controller;

import com.buba.springcloud.pojo.Dept;
import com.buba.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 张宇飞
 * @Description:
 * @date 2021/8/912:00
 */

@RestController
public class DeptController {
    private DeptService deptService;

    private DiscoveryClient client;

    @PostMapping("/dept/addDept")
    public boolean addDept(Dept dept) {
        return deptService.addDept(dept);
    }

    @GetMapping("/dept/get/{deptId}")
    public Dept selDeptById(@PathVariable("deptId") Long deptId) {
        return deptService.selDeptById(deptId);
    }

    @GetMapping("/dept/list")
    public List<Dept> selDept() {
        return deptService.selDept();
    }

    /**
     * @return 返回该服务的信息
     */
    @GetMapping("/dept/discovery")
    public Object discovery() {
        // 获得微服务列表清单
        List<String> services = client.getServices();
        System.out.println("services = " + services);
        // 得到一个具体的微服务信息
        List<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER-DEPT");

        for (ServiceInstance instance : instances) {
            System.out.println(instance.getHost() + "\t"
                    + instance.getPort() + "\t"
                    + instance.getUri() + "\t"
                    + instance.getServiceId());
        }
        return this.client;
    }

    @Autowired
    public void setClient(DiscoveryClient client) {
        this.client = client;
    }

    @Autowired
    public void setDeptService(DeptService deptService) {
        this.deptService = deptService;
    }

}
